package com.linkage.jz.bankruptcy.activity.Vote;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.widget.CommonPromptDialog;

import org.greenrobot.eventbus.EventBus;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author AFAP
 * 修改表决人结果
 */
public class ProVoteChangeResultActivity extends BaseActivity implements View.OnClickListener {
    public static final String KEY_ID = "KEY_ID";
    public static final String KEY_NAME = "KEY_NAME";
    public static final String KEY_ACTION = "KEY_ACTION";


    private String mId;
    private int mOpinion = 1;

    private EditText et_vote_note;


    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pro_vote_change_voter_result);
        super.onCreate(savedInstanceState);
        if (TextUtils.equals(getIntent().getStringExtra(KEY_ACTION), "new")) {
            titlebar_title.setText("表决");
        } else {
            titlebar_title.setText("修改" + getIntent().getStringExtra(KEY_NAME) + "的表决意见");
        }


        mId = getIntent().getStringExtra(KEY_ID);

        initViews();

        CommonPromptDialog mPromptDialog = new CommonPromptDialog(this, CommonPromptDialog.TYPE_TIPS);
        mPromptDialog.setContent("请确认已获得有效授权");
        mPromptDialog.setPositiveClickListener("是", view -> {
            mPromptDialog.dismiss();
        });
        mPromptDialog.setNegativeClickListener("否", view -> {
            mPromptDialog.dismiss();
            finish();
        });
        mPromptDialog.show();
    }

    private void initViews() {
        titlebar_menu_text.setVisibility(View.VISIBLE);
        titlebar_menu_text.setOnClickListener(this);

        et_vote_note = findViewById(R.id.et_vote_note);
        ((RadioGroup) findViewById(R.id.rg_vote_result)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rg_vote_result_1:
                        mOpinion = 1;
                        break;

                    case R.id.rg_vote_result_2:
                        mOpinion = 0;
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_menu_text:
                saveData();
                break;
        }
    }


    private void saveData() {
        String note = et_vote_note.getText().toString().trim();

//        if (TextUtils.isEmpty(note)) {
//            ToastUtil.showShort("补充不能为空");
//            return;
//        }

        showLoading("提交中...");
        Network
                .getAPIService()
                .changeOponion_Manager(getUserToken(), mId, mOpinion, note)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }

                        RefreshEvent event = new RefreshEvent();
                        event.setType(RefreshEvent.TYPE_VOTER_DETAILS);
                        EventBus.getDefault().post(event);

                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mShapeLoadingDialog.dismiss();
                        super.onError(e);
                    }
                });
    }
}
